Thread: string comparison

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    47

    Question string comparison

    I want to compare 2 strings. One is from user and one will be from a file.
    My file contains this.

    Philippines
    Olives
    Polymerization
    Coconut

    I dont get my expected output. Here's my code. Pls help

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    #include<iostream>
    int main()
    {
     char answer[512];
     char str[512];
        
     printf("INPUT STRING: ");
     scanf("%s", &str);
        
     FILE *fp;
     fp=fopen("JL-answers[easy].txt", "r");
     if (fp==NULL)
     {
      printf("ERROR OPENING FILE");
     }
     fscanf(fp,"%s", answer);
     if(strcmp(str, answer)==0)
     printf("STRING FOUND.\n");
     else
     printf("STRING NOT FOUND.\n");
     fclose(fp);
     return 0; 
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What output do you expect, and what output are you getting? I ran a quick test and got the expected results.

    As of now, it only checks the first string in the file. If you wanted to check all strings from the file, you would have to use a loop to read each string and compare it with the user input.

    Also:
    - "iostream" is a C++ header - if you're writing C, remove that
    - "conio.h" is a non-standard header, which you're not using any functions from - remove that, too
    - You check if your file fails to open, but still continue with the program (and still try to access that file pointer). If the program relies on that file, it should end if the file fails to open successfully.
    - Work on making your formatting neat and consistent.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    Quote Originally Posted by Matticus View Post
    What output do you expect, and what output are you getting? I ran a quick test and got the expected results.

    As of now, it only checks the first string in the file. If you wanted to check all strings from the file, you would have to use a loop to read each string and compare it with the user input.

    Also:
    - "iostream" is a C++ header - if you're writing C, remove that
    - "conio.h" is a non-standard header, which you're not using any functions from - remove that, too
    - You check if your file fails to open, but still continue with the program (and still try to access that file pointer). If the program relies on that file, it should end if the file fails to open successfully.
    - Work on making your formatting neat and consistent.
    I am expecting that if i enter a string that is same as the strings contained in the file, the system will display STRING FOUND. But when i run it, i always get the "STRING NOT FOUND".

    Also, im just included iostream because in my previous programs, i always use system("cls"). Im using Orwell v5.7.1on windows 8.

    I apology for my typing. I just type it manually here.
    Last edited by Moon River; 09-17-2014 at 06:30 AM.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    [Edit]
    I just type it manually here.
    o_O

    Do not do that.

    You waste our time and yours when you type your code. You will, inevitably, introduce mistakes by typing which we will cover before whatever was actually your problem.

    Always copy and paste your code into the browser.
    [/Edit]

    Also, im just included iostream because in my previous programs, i always use system("cls").
    O_o

    It absolutely does not matter what you were doing in the past.

    The `iostream' header is a C++ header. If you are using C, you can't use the C++ `iostream' header. I'd even go so far as to say that if your installation is correctly compiling that code as C, you have reconfigured your environment settings.

    Also, the `system' function has nothing to do with the `iostream' header. That you could use `system' from `iostream' is coincidence. The correct header for `system' is `stdlib.h' (or `cstdlib' in C++) which you are already including. If you intend to be using C as said, nix the `iostream' inclusion.

    [Edit]
    If you are not using C, you should be using `fstream' and friends.
    [/Edit]

    Soma
    Last edited by phantomotap; 09-17-2014 at 06:39 AM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What exactly are you trying to "find"? Do you realize that the scanf() series of functions stop processing the strings when a white space character is encountered? Do you realize that using the scanf() series of functions to retrieve a C-string are dangerous if you don't limit the number of characters they will try to retrieve? Do you know there is difference between C and C++, they are two distinct languages.

    Jim

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Moon River View Post
    I am expecting that if i enter a string that is same as the strings contained in the file, the system will display STRING FOUND. But when i run it, i always get the "STRING NOT FOUND".
    Code:
    INPUT STRING: Philippines
    STRING FOUND.
    It works for me for "Philippines". Does this not work for you?

    As I already said, it won't check the rest of the names on the list since you're only reading the first string from the file (i.e. you're not using a loop).

  7. #7
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    Can you correct my code pls? Im just a 1st year IT student. Dont expect much from me. C is my first language and i begin creating programs for about a month or so.

  8. #8
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    I didnt configure anything. Its just that my system("pause") and system("cls") is not working without the iostream.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    All I did was remove:

    #include<iostream>
    #include<conio.h>

    And ran the code as is.

    You are not answering the question. Are you saying that entering "Philippines" doesn't work for you? (And if not, are you sure you're spelling it the same as in the text file)?

    Can you correct my code pls?
    The only other issue (the third point in my post #2 above) is easy to resolve. Ask specific questions if you're not sure what is being said.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Moon River View Post
    I didnt configure anything. Its just that my system("pause") and system("cls") is not working without the iostream.
    You don't need system("pause"), there are alternatives to keeping the console window open: FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com

    You don't really need system("cls") - why do you want to clear the screen anyway?

  11. #11
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    So i can use loop for the system to check to whole file? How can i do that? Please teach me.

  12. #12
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    I had a hard time speaking english though, let's just say that i want my program to read the string given by the user then compare it to the data contained by the file. If anything matches, then it will display "String found". Or else, "string not found"

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You need to learn on your own - we are here to help with specific problems, not to be teachers. Besides, a forum is a very poor medium for teaching new concepts.

    I'll offer what advice I can, though.

    If you are unfamiliar with loops, start by reading about them in your book, or other learning material. Tutorials are okay (like here), but nothing beats a good book for learning. Type out all the examples, read through them a few times to see what is happening, and make sure you understand the concepts. Tweaking the example code to see if what happens is what you expect also helps in understanding.

    Then you need to develop your logic. I really don't want to give away too much, since it would rob you of a learning experience, but I will try to give you a nudge without giving too much away.

    Code:
    /* clear flag */
    
    while( /* string successfully read */)
    {
        /* compare string with user input */
        /* if it matches, set flag */
    }
    
    /* if flag was set, a match was found */
    /* otherwise, a match was not found */
    That is the framework for what I believe you're asking. There are other ways you can go about it, too, this is just one possible way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Comparison
    By Darkobra in forum C Programming
    Replies: 7
    Last Post: 01-06-2012, 04:56 PM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. String Comparison
    By kpreston in forum C Programming
    Replies: 16
    Last Post: 10-20-2008, 07:43 PM
  4. string comparison
    By sj999 in forum C Programming
    Replies: 4
    Last Post: 05-23-2008, 03:10 AM
  5. please help with a string comparison.
    By MegaManZZ in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 01:33 PM